

var loadedImgeCount = 0;
var page = 0;


window.onload = function(){
    document.getElementsByClassName("content")[0].onscroll = function(){
        var st = document.getElementsByClassName("content")[0].scrollTop||document.documentElement.scrollTop;
        var ch = document.getElementsByClassName("content")[0].clientHeight||document.document.documentElement.clientHeight;
        var sh = document.getElementsByClassName("content")[0].scrollHeight||document.documentElement.scrollHeight;

        var isDaoDi = st + ch ==sh;
        if(isDaoDi&&!diXian){
            loadNextPageData();
        }
        loadRealImage();
    }
};

loadNextPageData();

var timer;
function toTop(){
    $("html,body").animate({ scrollTop: 0 }, 500);
    //  var st = document.body.scrollTop||document.documentElement.scrollTop;

    //  var speed = st/30;
    //  console.log(speed);
    // timer = setInterval(function(){
    //       if(st==0){
    //          clearInterval(timer);
    //     }
    //    var currentTop = parseInt(st);
    //      document.body.scrollTop = currentTop - speed;
    //      document.documentElement.scrollTop = currentTop - speed;
    //   },2)
}



function loadNextPageData(){
    page++;
    if(page>=2){
        loading2.style.display = "block";
    }
    var pageCount = (page-1)*44;

    $.ajax({
        url: "https://s.taobao.com/search?data-key=s%2Cps&data-value=0%2C1&ajax=true&_ksTS=1530701553891_1856&spm=a21bo.2017.201856-fline.1.5af911d9k14uIL&q=%E8%BF%9E%E8%A1%A3%E8%A3%99&refpid=420460_1006&source=tbsy&style=grid&tab=all&pvid=d0f2ec2810bcec0d5a16d5283ce59f66&bcoffset=0&p4ppushleft=3%2C44&s=44",
        dataType:"jsonp",
        success:function(data){
            //console.log(data);
            loadData(data);
        }
    });
}
var diXian;


function loadData(data){
    if(!data.mods.itemlist.data){
        if(!diXian){
            diXian = document.createElement("div");
            diXian.innerHTML = "我是有底线的";
            diXian.style.textAlign = "center";
            diXian.style.padding = "0.5rem";
            document.body.appendChlid(diXian);
            loading2.style.display = "none";
        }
        d1.style.display = "none";
        return;
    }


    var productArr = data.mods.itemlist.data.auctions;
    for(var i=0;i<productArr.length;i++){
        var p = productArr[i];

        var productDiv = document.createElement("div");
        productDiv.className = "product fl";

        var image = document.createElement("img");
        image.style.width="180px";
        image.style.height="180px";
        //image.src="http:"+p.pic_url;
        image.setAttribute("real_image_src","http:"+p.pic_url+"_180x180.jpg");
        image.src="images/dot.png";

        var titleDiv = document.createElement("div");
        titleDiv.innerHTML = p.title;
        titleDiv.style.fontSize="12px";
        titleDiv.style.height="1.9rem";
        titleDiv.style.overflow="hidden";
        titleDiv.style.width="98%";

        var priceDiv = document.createElement("div");
        priceDiv.innerHTML = p.view_price;
        priceDiv.style.fontSize="14px";
        priceDiv.style.color="#e4393c";

        productDiv.appendChild(image);
        productDiv.appendChild(titleDiv);
        productDiv.appendChild(priceDiv);

        productDiv.p = p;
        productDiv.onclick = function(){
            localStorage.setItem("currentP",JSON.stringify(this.p));
            location = "item.html";
        };

        productList.appendChild(productDiv);

    }
    loadedImageCount = 0;
    $("#productList img").load(
        function(){
            loadedImageCount++;
            if(productArr.length==loadedImageCount){
                //console.log("所有图片加载完毕！");
                $("#productList").fadeIn(400);
                loadRealImage();
                d1.style.display = "none";
            }
        }
    );
}
function loadRealImage(){
    var images = document.getElementsByTagName("img");

    var st = document.documentElement.scrollTop;
    var ch =document.documentElement.clientHeight;
    var sh = document.documentElement.scrollHeight;

    for(var i=0;i<images.length;i++){
        var picParent  = images[i].parentNode;
        var eleheight = $(picParent).height();
        var eleOffsetTop = $(picParent).offset().top;

        console.log(eleheight);
        console.log(eleOffsetTop);
        if(eleheight+eleOffsetTop-st<ch){
            if(images[i].getAttribute("real_image_src")){
                images[i].src = images[i].getAttribute("real_image_src");
                images[i].setAttribute("real_image_src","");
                images[i].style.opacity = 1;
            }
        }
    }
}
loadRealImage();
